
$(function () {
    var load = $("#load").val();
    if ("detail" == load) {//是查看
        $("input,textarea,select").attr("readonly", "readonly");
    } else {
        //初始化事件控件
        /* layui.use('laydate', function () {
             var laydate = layui.laydate;
             //日期时间选择器
             laydate.render({
                 elem: '#buildtime'
                 , type: 'month'
             });
         });*/
        $("input,textarea,select").removeAttr("readonly");
        if("update" == load){
            $("#userName").attr("readonly", "readonly");
        }
    }
});

/**添加或者修改的方法*/
function saveOrUpdate() {
    if (checkForm() == false) {
        return false;
    }
    var res = false;
    $.ajax({
        url: "user/opt",
        type: "post",
        cache: false,
        async: false,
        data: $("#model").serialize(),
        dataType: "json",
        success: function (data) {
            if (data==1) {
                parent.layer.msg('操作成功！', {icon: 1});
                res = true;
            } else {
                parent.layer.msg('操作失败：' + data.msg, {icon: 5});
            }
        },
        error: function (data) {
            parent.layer.msg('系统异常！', {icon: 1});
        }
    });
    return res;
}

function checkForm() {
    var userName = $("#userName").val();
    var password = $("#password").val();
    var parten = /^.{1,200}$/;//标题不能超过200个字
    if (!userName.match(parten)) {
        parent.layer.msg('用户名在1~200个字符之内！');
        return false;
    }
    if(isExist(userName)==true){
        parent.layer.msg('该用户名已经被注册');
        return false;
    }
    if (!password.match(parten)) {
        parent.layer.msg('密码在1~200个字符之内！');
        return false;
    }
    return true;
}


function isExist(userName) {
    var load = $("#load").val();
    if ( "add" != load  ) {// 不是添加，不做判断
       return false;
    }
    var res = true;
    $.ajax({
        url: "user/check",
        type: "post",
        cache: false,
        async: false,
        data: {"userName":userName},
        dataType: "json",
        success: function (data) {
            res = data.msg;
        }
    });
    return res;
}
